<%-- 
    Document   : savemodel
    Created on : Apr 23, 2012, 10:47:53 AM
    Author     : Winzelric
--%>

<%@page import="algorithm.KNearestNeighbourModel"%>
<%@page import="algorithm.DecisionTreeModel"%>
<%@page import="algorithm.NaiveBayesianModel"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page  import="java.io.FileInputStream" %>
<%@ page  import="java.io.BufferedInputStream"  %>
<%@ page  import="java.io.File"  %>
<%@ page import="java.io.IOException" %>


<%

  // you  can get your base and parent from the database
  String filename = request.getParameter("filename");
// you can  write http://localhost

  BufferedInputStream buf = null;
  ServletOutputStream myOut = null;

  try {

    String type = (String) session.getAttribute("type");
    String algorithm = (String) session.getAttribute("algorithm");
    Object o;

    if (algorithm.equals("ori")) {
      if (type.equals("nb")) {
        NaiveBayesianModel nbm = (NaiveBayesianModel) session.getAttribute("model");
        nbm.saveModelWEKA(application.getRealPath("/") + filename + ".model");
      } else if (type.equals("dt")) {
        DecisionTreeModel dtm = (DecisionTreeModel) session.getAttribute("model");
        dtm.saveModelWEKA(application.getRealPath("/") + filename + ".model");
      } else {
        KNearestNeighbourModel knnm = (KNearestNeighbourModel) session.getAttribute("model");
        knnm.saveModelWEKA(application.getRealPath("/") + filename + ".model");
      }
    } else {
      if (type.equals("nb")) {
        NaiveBayesianModel nbm = (NaiveBayesianModel) session.getAttribute("model");
        nbm.saveModel(application.getRealPath("/") + filename + ".model");
      } else if (type.equals("dt")) {
        DecisionTreeModel dtm = (DecisionTreeModel) session.getAttribute("model");
        dtm.saveModel(application.getRealPath("/") + filename + ".model");
      } else {
        KNearestNeighbourModel knnm = (KNearestNeighbourModel) session.getAttribute("model");
        knnm.saveModel(application.getRealPath("/") + filename + ".model");
      }
    }

    myOut = response.getOutputStream();
    File myfile = new File(application.getRealPath("/") + filename + ".model");

    //set response headers
    response.setContentType("text/plain");

    response.addHeader(
            "Content-Disposition", "attachment; filename=" + filename + ".model");

    response.setContentLength((int) myfile.length());

    FileInputStream input = new FileInputStream(myfile);
    buf = new BufferedInputStream(input);
    int readBytes = 0;

    //read from the file; write to the ServletOutputStream
    while ((readBytes = buf.read()) != -1) {
      myOut.write(readBytes);
    }

  } catch (IOException ioe) {

    throw new ServletException(ioe.getMessage());

  } finally {

    //close the input/output streams
    if (myOut != null) {
      myOut.close();
    }
    if (buf != null) {
      buf.close();
    }

  }



%>
